package com.dynamic.model.service.impl;

import com.dynamic.model.common.dao.BaseDao;
import com.dynamic.model.common.service.impl.BaseServiceImpl;
import com.dynamic.model.dao.RequestHeadDao;
import com.dynamic.model.model.po.RequestHead;
import com.dynamic.model.model.po.SysmanUser;
import com.dynamic.model.service.RequestServiceI;
import com.dynamic.model.util.redis.CacheManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @desc 被动请求serviceImpl类
 *
 * @author luwei
 * @Fri Aug 08 14:25:29 CST 2014
 */
@Service
public class RequestServiceImpl extends BaseServiceImpl<RequestHead> implements RequestServiceI {

	@Resource(name ="requestHeadDaoImpl")
	public void setBaseDao(BaseDao<RequestHead> dao) {
		this.baseDao =  dao;
	}

	private RequestHeadDao getDao(){
		return (RequestHeadDao) this.baseDao ;
	}

	@Override
	public void flushCache() {
		Subject subject = SecurityUtils.getSubject();
		SysmanUser user = (SysmanUser) subject.getPrincipal();
		String host=user.getHost();
		List<RequestHead> requests=this.getDao().getRequestListByUser(user.getPid());
		for(int i=0;i<requests.size();i++){
			Map<String,RequestHead> map=new HashMap<>();
			map.put(requests.get(i).getRequestDomain()+requests.get(i).getRequestUrl(),requests.get(i));
			CacheManager.getInstance().setAttributeMapGlobal(host,map);
		}
	}
}
